﻿using Humanizer;
using System;
using System.Text.RegularExpressions;

namespace Msp.Scaffolding.Entities
{
    /// <summary>
    /// 属性
    /// </summary>
    public class PropertyInfo
    {
        public PropertyInfo(string type, string name)
        {
            Type = type;
            Name = name;
        }

        public PropertyInfo(string type, string name, string remarks) : this(type, name)
        {
            Remarks = remarks ?? throw new ArgumentNullException(nameof(remarks));
        }

        /// <summary>
        /// 代码包含注释
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 是否继承类里的属性，比如接口里的属性。
        /// </summary>
        public bool IsVirtual { get; set; }


        public string GetDtoCode
        {
            get
            {
                var code = Regex.Replace(FullName, @"\[Column(.*?)]|\[(.*?)Column(.*?)]", "");



                code = Regex.Replace(code, @"\ #region.*?\S+", "");

                code = Regex.Replace(code, @"\ #endregion*?\S+", "");

                code = Regex.Replace(code, @"\[Other(.*?)]|\[(.*?)Other(.*?)]", "");
                //如果不是基类型则把类型的尾加个 Dto
                if (!IsBaseType)
                {
                    //  public Inbound Inbound { get; set; } = new  Inbound();
                    //  code = Regex.Replace(FullName, @"\public (.*?) \S", "");

                    var s = (Regex.Match(code, @"public (.*?) \S")?.Value ?? "").Replace(this.Name, this.Name + "");

                    code = Regex.Replace(code, @"public (.*?) \S", s);


                    var s1 = (Regex.Match(code, @"new (.*?)\;")?.Value ?? "").Replace(this.Name, this.Name + "");

                    code = Regex.Replace(code, @"new (.*?)\;", s1);

                }
                return code;
            }
        }

        public bool IsBaseType
        {
            get
            {
                var t = this.Type;
                if (t.ToLower().Contains("long") || t.ToLower().Contains("int") || t.ToLower().Contains("string") || t.ToLower().Contains("decimal")
               || t.ToLower().Contains("double") || t.ToLower().Contains("float") || t.ToLower().Contains("datetime")
               || t.ToLower().Contains("dateonly") || t.ToLower().Contains("timeonly") || t.ToLower().Contains("timespan")
               || t.ToLower().Contains("dateonly") || t.ToLower().Contains("timeonly") || t.ToLower().Contains("timespan")

               )
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 属性类型
        /// </summary>
        public string Type { get; }

        public string TypeLower
        {
            get
            {
                return Type.ToLower();
            }
        }
        /// <summary>
        /// ts类型
        /// </summary>
        public string TsType
        {
            get
            {
                if (Type.ToLower().Contains("long") || Type.ToLower().Contains("int") || Type.ToLower().Contains("double") || Type.ToLower().Contains("float"))
                {
                    return "number";
                }
                else if (Type.ToLower().Contains("string") || Type.ToLower().Contains("datetime")) return "string";
                return "any";
            }
        }
        /// <summary>
        /// 是否浮点数
        /// </summary>
        public bool IsFloat
        {
            get
            {
                if (Type.ToLower().Contains("decimal") || Type.ToLower().Contains("double") || Type.ToLower().Contains("float"))
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否整形
        /// </summary>
        public bool IsInt
        {
            get
            {
                if (Type.ToLower().Contains("long") || Type.ToLower().Contains("int"))
                {
                    return true;
                }
                return false;
            }
        }

        public bool IsBool
        {
            get
            {
                if (Type.ToLower().Contains("bool") || Type.ToLower().Contains("Boolean"))
                {
                    return true;
                }
                return false;
            }
        }
        /// <summary>
        /// 是否可空类型
        /// </summary>
        public bool HasNullable { get; set; }

        /// <summary>
        /// 属性名
        /// </summary>
        public string Name { get; }

        #region 字段信息

       /* /// <summary>
        /// 字段属性，来用生成表单
        /// </summary>
        public FieldAttribute FieldInfo { get; set; }*/


        /// <summary>
        /// 表单输入类型，input inputnumber
        /// </summary>
        public string InputType { get; set; }


        #endregion




        public string Format { get; set; }

        /// <summary>
        /// 显示名称
        /// </summary>
        public string? DisplayName { get; set; }

        /// <summary>
        /// 属性名小写
        /// </summary>
        public string NameCame => Name.Camelize();


        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }


        /// <summary>
        /// 属性名转下划线
        /// </summary>
        public string NameUnderscore => Name.Underscore();





        /// <summary>
        /// 是否有最大长度
        /// </summary>
        public bool HasMaxLength
        {
            get
            {
                if (this.MaxLength.HasValue && this.MaxLength.Value > 0) return true;
                if (this.StringLength_Max.HasValue && this.StringLength_Max.Value > 0) return true;
                return false;
            }
        }
        /// <summary>
        /// 最大长度
        /// </summary>
        public int MaxLen
        {
            get
            {
                if (this.MaxLength.HasValue && this.MaxLength.Value > 0) return this.MaxLength.Value;
                if (this.StringLength_Max.HasValue && this.StringLength_Max.Value > 0) return this.StringLength_Max.Value;
                return 100;
            }
        }



        /// <summary>
        /// 字符串长度。
        /// </summary>
        public int? StringLength_Min { get; set; }

        public int? StringLength_Max { get; set; }

        public string? StringLength_Msg { get; set; }

        /// <summary>
        /// 数字长度限制
        /// </summary>
        public int? Range_Min { get; set; }

        public int? Range_Max { get; set; }

        public string? Range_Msg { get; set; }



        /// <summary>
        /// 最大长度
        /// </summary>
        public int? MaxLength { get; set; }
        /// <summary>
        /// 验证消息
        /// </summary>
        public string? MaxLengthMsg { get; set; }

        /// <summary>
        /// 最小长度
        /// </summary>
        public int? MinLength { get; set; }
        public string? MinLength_Msg { get; set; }

        /// <summary>
        /// 比较字段
        /// </summary>
        public string? Compare { get; set; }
        public string? Compare_Msg { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; }

        public string DataType_Msg { get; set; }
        /// <summary>
        /// 远程验证
        ///[Remote(nameof(RoleController.CheckRoleExist), "Role", ErrorMessage = "角色已存在")]
        /// public string a { get; set; }
        /// </summary>
        public string Remote { get; set; }
        public string Remote_msg { get; set; }

        /// <summary>
        /// 是否必填项
        /// </summary>
        public bool? Required { get; set; }
        public string Required_msg { get; set; }

        /// <summary>
        /// 自定义验证表达式。
        /// </summary>
        public string RegularExpression { get; set; }

        public string RegularExpressionMsg { get; set; }

        public bool HasVail
        {
            get
            {
                if (!HasNullable || Required.GetValueOrDefault() || StringLength_Min.HasValue || MinLength.HasValue || MaxLength.HasValue || !Compare.IsNullOrEmpty()
                  || !Remote.IsNullOrEmpty() || Required.HasValue || !RegularExpression.IsNullOrEmpty())
                    return true;
                else
                    return false;
            }
        }

    }
}
